//用带(头)尾指针的循环链表表示队列，写出出队入队算法
#include<stdio.h>
#include<stdlib.h>

typedef struct Node{
    int data;
    struct Node* next;
}Node; 

typedef struct Queue
{
    struct Node *front,*rear;
    
}Queue;
int initQueue(Queue* q);
int push(Queue* q,int data);
int pull(Queue* q,int* data);
int main(){
    Queue q;
    initQueue(&q);
    push(&q,1);
    push(&q,2);
    int a,b;
    pull(&q,&a);
    pull(&q,&b);
    printf("res:%d,%d",a,b);
    return 0;
}

int initQueue(Queue* q){
    q->front=q->rear=(Node*)malloc(sizeof(Node));
    q->rear->next=q->front;
    return 1;
}

int push(Queue* q,int data){
    Node* newNode = (Node*)malloc(sizeof(Node));
    newNode->data=data;
    newNode->next=q->front;
    q->rear->next=newNode;
    q->rear=newNode;
    return 1;
}

int pull(Queue* q,int* data){
    //队头等于队尾，队列为空返回0
    if(q->front==q->rear){
        return 0;
    }
    Node* p = q->front->next;
    q->front->next=q->front->next->next;
    q->rear->next=q->front;
    *data=p->data;
    free(p);
    return 1;
}